﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace DependencyPropertyTestDemo
{
    /// <summary>
    /// 附加依赖属性
    /// </summary>
    class PasswordAttached : DependencyObject
    {
        public static string GetPassword(DependencyObject obj)
        {
            return (string)obj.GetValue(PasswordProperty);
        }

        public static void SetPassword(DependencyObject obj, string value)
        {
            obj.SetValue(PasswordProperty, value);
        }

        // Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordAttached), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Password_OnPropertyChanged));


        private static bool _isUpdating = false;

        private static void Password_OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var pb = d as PasswordBox;

            if (pb is null || string.IsNullOrWhiteSpace((string)e.NewValue))
                return;

            if (!_isUpdating)
            {
                pb.PasswordChanged -= Pb_PasswordChanged;
                pb.Password = e.NewValue.ToString();
                pb.PasswordChanged += Pb_PasswordChanged;

            }

            //tmp.Password = (string)e.NewValue;
            //PasswordAttached.SetPassword(d, (string)e.NewValue);
            //var password = PasswordAttached.GetPassword(d);
        }

        private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
        {
           PasswordBox pb = sender as PasswordBox;

            _isUpdating = true;
            SetPassword(pb, pb.Password);
            _isUpdating = false;
        }



        public static bool GetAttach(DependencyObject obj)
        {
            return (bool)obj.GetValue(AttachProperty);
        }

        public static void SetAttach(DependencyObject obj, bool value)
        {
            obj.SetValue(AttachProperty, value);
        }

        // Using a DependencyProperty as the backing store for Attach.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AttachProperty =
            DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordAttached), new PropertyMetadata(false, OnAttachChanged));

        /// <summary>
        /// Attach属性改变回调，因为必须要使用Atatach事件去驱动PasswordBox的PasswordChanged事件
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox pb = d as PasswordBox;
            if (pb == null) return;

            // 给PasswordBox注册一个事件
            pb.PasswordChanged += Pb_PasswordChanged;
        }

    }
}
